<?php
/**
 * (ajax) 代发（一件代发）
 */

namespace App\Admin\Ajax\OverSeasBa;


use App\Models\OverSeasBa\DropshippingInventoryFnsku;
use App\Models\OverSeasBa\RemovaleInventoryFNSku;
use App\Models\OverSeasBa\RemovaleLog;
use Illuminate\Support\Facades\DB;

class DropshippingInventoryFnSkuAjax{

    /**
     * 库存转移
     * @param $param
     * @return \Illuminate\Http\JsonResponse
     */
    public function fnSkuMigrate($param){

        DB::beginTransaction();
        try{
            $detail = $param['detail'];
            $msg = [];
            foreach($detail as $item){

                $dropshippingId = $item['id'];
                $migrateNum = $item['migrate_num'];
                if (!is_numeric($migrateNum)) {
                    DB::rollBack();
                    continue;
                }

                //代发
                $dropshippingFnSku = DropshippingInventoryFnSku::query()->where('id', $dropshippingId)->lockForUpdate()->first();
                if(!$dropshippingFnSku){
                    DB::rollBack();
                    continue;
                }
                if($dropshippingFnSku->instant < $migrateNum){
                    $msg[] = $dropshippingFnSku->fnsku;
                    DB::rollBack();
                    continue;
                }
                $dropshippingFnSku->instant -= $migrateNum;
                $flag1 = $dropshippingFnSku->save();
                if(!$flag1){
                    DB::rollBack();
                    continue;
                }

                //移库
                $removaleFnSku = RemovaleInventoryFNSku::query()
                    ->where([
                        'warehouse_id' => $dropshippingFnSku->warehouse_id,
                        'account_id' => $dropshippingFnSku->account_id,
                        'fnsku' => $dropshippingFnSku->fnsku
                    ])
                    ->lockForUpdate()
                    ->first();

                if ($removaleFnSku) {
                    $afterNum = $removaleFnSku->getOriginal('instant');
                    $removaleFnSku->instant += $migrateNum;
                    $flag2 = $removaleFnSku->save();
                    if(!$flag2){
                        DB::rollBack();
                        continue;
                    }
                }else{
                    $afterNum = 0;
                    $removaleFnSku = RemovaleInventoryFNSku::query()->create([
                        'warehouse_id' => $dropshippingFnSku->warehouse_id,
                        'account_id' => $dropshippingFnSku->account_id,
                        'fnsku' => $dropshippingFnSku->fnsku,
                        'psku_id' => $dropshippingFnSku->psku_id,
                        'instant' => $migrateNum
                    ]);
                    if(!$removaleFnSku){
                        DB::rollBack();
                        continue;
                    }
                }

                //日志
                RemovaleLog::logs([
                        'warehouse_id' => $removaleFnSku->warehouse_id,
                        'account_id' => $removaleFnSku->account_id,
                        'fnsku' => $removaleFnSku->fnsku,
                        'psku_id' => $removaleFnSku->psku_id,
                        'num' => $migrateNum,
                        'after_num' => $afterNum,
                        'before_num' => $removaleFnSku->instant
                    ]);

                DB::commit();
            }

            return response()->json([
                'status' => !empty($msg) ? false : true,
                'message' => !empty($msg) ? implode(PHP_EOL, $msg).'数量异常' : '操作成功！'
            ]);
        }catch (\Exception $e){
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }

}


